package org.shock.ss.sms.action;

import java.io.UnsupportedEncodingException;
import java.sql.ResultSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import org.shock.ss.sms.bean.Pager;
import org.shock.ss.sms.bean.system.ProjectLog;
import org.shock.ss.sms.bean.system.UserInfor;

import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	protected Logger log = Logger.getLogger(getClass());
	protected Boolean success = true;
	protected String message = "";// 正常响应消息
	protected String errMsg = "";// 错误消息
	protected String errCode = "";// 错误号
	protected String jmpUrl = "javascript:history.go(-1)";// 页面跳转url
	protected String nav = "";// 导航信息
	protected String pageStr;// 分页显示
	protected String jsonString = "{success:true}";
	protected StringBuffer sqladd = new StringBuffer("");// 查询附加条件
	protected String dir = "desc";// 升序降序 默认升序
	protected String sort = "id";// 排序字段 默认为id
	protected Pager pager = new Pager();
	protected int totalCount = 0;
	protected int pageno = 1;// 当前页
	protected int pagecount = 0;// 最大数
	protected int rownum = 15;// 每页显示数
	protected int pagerow = 10;// 每页记录数
	protected int page_count;// 页面显示
	protected int start = 0;// ext 分页参数
	protected int limit = 10;// ext 分页参数
	protected int page;// 分页
	protected String action = "";
	protected String op = "";// 区分页面是正常请求还是ajax请求
	protected ProjectLog prjlog = new ProjectLog("系统日志", "", "", getRequest().getRemoteAddr(), "", "");
	protected String result;
	protected int jmptime = 3;// 消息页面跳转时间 单位 秒！

	/**
	 * 获取Object[]结果
	 */
	public String message() {
		return SUCCESS;
	};

	public Object[] getObjectResult(ResultSet rs, String result, int length) throws Exception {
		String keys[] = result.split(",");
		Object[] obj = new Object[keys.length + length];
		for (int i = 0; i < keys.length; i++) {
			obj[i] = rs.getString(keys[i]);
		}
		return obj;
	}

	/**
	 * 获取 response
	 * 
	 * @return 当前 response
	 */
	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	/**
	 * 获取 request
	 * 
	 * @return 当前 request
	 */
	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * 获取session
	 */
	protected HttpSession getSession() {
		return getRequest().getSession();
	}

	protected UserInfor getUserInfor() {
		return (UserInfor) getSession().getAttribute("userinfor");
	}

	/**
	 * 获取ip
	 */
	public String getIp() {
		return ServletActionContext.getRequest().getRemoteAddr();
	}

	/** 当页面传字符串，而字符串中可能含有汉字是必须使用此转换字母编码集以避免出现乱码 */
	public String URLEncoder(String s) {
		if (s == null) {
			s = "";
		}
		try {
			s = java.net.URLEncoder.encode(s, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return s;
	}

	public String getPageSql(String sql) {
		String newsql = "select * from (select rownum r_id,t.*  from (" + sql + ") t where rownum <= "
				+ (pageno * rownum) + ") where r_id >" + ((pageno - 1) * rownum);
		return newsql;
	}

	public String getPageStr() {
		return pageStr;
	}

	public void setPageStr(String pageStr) {
		this.pageStr = pageStr;
	}

	public int getPageno() {
		return pageno;
	}

	public void setPageno(int pageno) {
		this.pageno = pageno;
	}

	public int getPagecount() {
		return pagecount;
	}

	public void setPagecount(int pagecount) {
		this.pagecount = pagecount;
	}

	public int getRownum() {
		return rownum;
	}

	public void setRownum(int rownum) {
		this.rownum = rownum;
	}

	public int getJmptime() {
		return jmptime;
	}

	public void setJmptime(int jmptime) {
		this.jmptime = jmptime;
	}

	public int getPagerow() {
		return pagerow;
	}

	public void setPagerow(int pagerow) {
		this.pagerow = pagerow;
	}

	public int getPage_count() {
		return page_count;
	}

	public void setPage_count(int page_count) {
		this.page_count = page_count;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public Logger getLog() {
		return log;
	}

	public void setLog(Logger log) {
		this.log = log;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getErrMsg() {
		return errMsg;
	}

	public void setErrMsg(String errMsg) {
		this.errMsg = errMsg;
	}

	public String getErrCode() {
		return errCode;
	}

	public void setErrCode(String errCode) {
		this.errCode = errCode;
	}

	public String getJmpUrl() {
		return jmpUrl;
	}

	public void setJmpUrl(String jmpUrl) {
		this.jmpUrl = jmpUrl;
	}

	public String getJsonString() {
		return jsonString;
	}

	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}

	

	public void setNav(String nav) {
		this.nav = nav;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Boolean getSuccess() {
		return success;
	}

	public void setSuccess(Boolean success) {
		this.success = success;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String getOp() {
		return op;
	}

	public void setOp(String op) {
		this.op = op;
	}

	public ProjectLog getPrjlog() {
		return prjlog;
	}

	public void setPrjlog(ProjectLog prjlog) {
		this.prjlog = prjlog;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

}
